<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" />
    <h1>{{ count }}</h1>
    <h1>{{ double }}</h1>
    <h1>{{ greetings }}</h1>
    <h1>X:{{ x }},Y:{{ y }}</h1>
    <button @click="openModal">open modal</button>
    <modal :isOpen="modalIsOpen" @close-modal="closeModal">my modal~</modal>
    <button @click="increase">+1</button>
    <button @click="updateGreeting">Update Title</button>
    <h1 v-if="loading">正在loading</h1>
    <img v-if="loaded" :src="result[0].url" />
    <Suspense>
      <template #default>
        <div>
          <async-show />
          <dog-show />
        </div>
      </template>
      <template #fallback>
        <h1>loading~</h1>
      </template>
    </Suspense>
    <p>{{ error }}</p>
  </div>
</template>

<script lang="ts">
import { ref, computed, reactive, toRefs, watch, onErrorCaptured } from "vue";
import useMousePosition from "./hooks/useMousePosition";
import useURLLoader from "./hooks/useURLLoader";
import Modal from "./components/modal.vue";
import AsyncShow from "./components/AsyncShow.vue";
import DogShow from "./components/DogShow.vue";
interface DataProps {
  count: number;
  double: number;
  increase: () => void;
}

interface DogResult {
  message: string;
  status: string;
}

interface CatResult {
  id: string;
  url: string;
  width: number;
  height: number;
}

export default {
  name: "App",
  components: {
    Modal,
    AsyncShow,
    DogShow,
  },
  setup() {
    const error = ref(null);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    onErrorCaptured((e: any) => {
      // 把错误传出 赋值给 e
      error.value = e;
      // return 一个 Boolean 值表示要不要将错误数据向上传出
      return true;
    });
    // const count = ref(0);
    // const increase = () => {
    //   count.value++;
    // };
    // const double = computed(() => {
    //   return count.value * 2;
    // });
    const data: DataProps = reactive({
      count: 0,
      increase: () => {
        data.count++;
      },
      double: computed(() => data.count * 2),
    });

    const greetings = ref("");
    const updateGreeting = () => {
      greetings.value += "Hello!";
    };

    const { x, y } = useMousePosition();
    // // dog
    // const { result, loading, loaded } = useURLLoader<DogResult>(
    //   "https://dog.ceo/api/breeds/image/random"
    // );
    // // watch 最新的变化
    // watch(result, () => {
    //   if (result.value) {
    //     console.log("value", result.value.message);
    //   }
    // });
    // cat
    const { result, loading, loaded } = useURLLoader<CatResult[]>(
      "https://api.thecatapi.com/v1/images/search?limit=1"
    );
    watch(result, () => {
      if (result.value) {
        console.log("value", result.value[0].url);
      }
    });
    // watch([greetings, () => data.count], (newValue, oldValue) => {
    //   console.log("old", oldValue);
    //   console.log("new", newValue);
    //   document.title = "updated" + greetings.value + data.count;
    // });

    const refData = toRefs(data);
    const modalIsOpen = ref(false);
    const openModal = () => {
      modalIsOpen.value = true;
    };
    const closeModal = () => {
      modalIsOpen.value = false;
    };
    return {
      ...refData,
      greetings,
      updateGreeting,
      x,
      y,
      result,
      loading,
      loaded,
      modalIsOpen,
      openModal,
      closeModal,
      error,
      onErrorCaptured,
    };
  },
};
</script>